package com.phone.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.phone.entity.Product;
import com.phone.entity.Store;
import com.phone.service.StoreService;
import com.phone.service.UserService;

@Controller
public class StoreController {
	@Resource
	private UserService userServiceImpl;
	@Resource
	private StoreService serviceimpl;
	@RequestMapping("addproduct")
	public String addproduct(HttpServletRequest request,Product product,HttpSession session,MultipartFile img) throws IOException{
		//获取文件名称
		String filenames=img.getOriginalFilename();
		String filename=UUID.randomUUID().toString();
		//String pt = session.getServletContext().getRealPath("images")+File.separator+filename;//存入tomacat里
		String path = "E:\\JAVA开发软件包\\2017级java开发软件包\\新建文件夹\\PhoneStore\\WebContent\\images\\";
		String name=filenames.substring(filenames.lastIndexOf("."));//获取文件拓展名
		//获取输入流
		InputStream is=img.getInputStream();
		//获取输出流
		File file=new File(path+filename+name);
		//File files=new File(pt);
		FileOutputStream os=new FileOutputStream(file);
		//FileOutputStream oss=new FileOutputStream(files);
		int i=0;
		while((i=is.read())!=-1){
			os.write(i);
			//oss.write(i);
		}
		os.close();
		//oss.close();
		is.close();
		product.setListimg(filename+name);
		String username = request.getParameter("username");
		Store store=userServiceImpl.seles(username);
		product.setStore(store);
		serviceimpl.addproduct(product);
		int sid=store.getId();
		List<Product> products= serviceimpl.showproduct(sid);
		session.setAttribute("store", store);
		session.setAttribute("products", products);
		return "/storeshop2.jsp";
	}
	@RequestMapping("shangpin")
	public String shangpin(HttpServletRequest request,Model model){
		String username = request.getParameter("username");
		Store store=userServiceImpl.seles(username);
		model.addAttribute("store", store);
		return "/addproduct.jsp";
	}
	@RequestMapping("showproduct")
	public String showproduct(HttpServletRequest request,HttpSession session){
		String username=request.getParameter("username");
		Store store=userServiceImpl.seles(username);
		int sid=store.getId();
		List<Product> products= serviceimpl.showproduct(sid);
		if (products==null || products.size()==0) {
			session.setAttribute("message", "你的商店没有东西!");
			return "allproduct";
		}else {
			session.setAttribute("store", store);
			session.setAttribute("products", products);
			return "/storeshop2.jsp";
		}
	}
	@RequestMapping(value="upstore",method=RequestMethod.POST)
	public String upstore(HttpSession session,Store store,HttpServletRequest request){
		serviceimpl.upstore(store);
		session.setAttribute("message", "修改成功!");
		return "allproduct";
	}
	
}
